$(document).ready(function() {
	     //开始就判断是否有这两个数组，没有就新建
		let studentarr = JSON.parse(localStorage.getItem("studentarr"));
		if (!studentarr) {
			studentarr = new Array();
		};
		let adminarr = JSON.parse(localStorage.getItem("adminarr"));
		if (!adminarr) {
			adminarr = new Array();
		};
		/*登录功能*/
		
		// let login_account = $("input[id='login_account']");
		// console.log(login_account);
		// let login_password = $("input[id='login_password']");
		//记住密码
		let remccount=JSON.parse(localStorage.getItem("remccount"));
		if (remccount) {
			login_account=$("input[id='login_account']").val(remccount.cu_user);
			login_password=$("input[id='login_password']").val(remccount.cu_password);
			$("input[name='cb_rem']").attr("checked",true);
			if (remccount.yhlx==="admin") {
				$("input[value='admin']").attr("checked",true);
			} 
			if(remccount.yhlx==="administrators"){
				$("input[value='administrators']").attr("checked",true);
			}
			if (remccount.yhlx==="student") {
				$("input[value='student']").attr("checked",true);
			}
		}
		// //获取记住密码框
		// let remember=null;
		// $("input[id='cb_rem']").change(function() {
		// 	 remember=$(this).val();
		// 	console.log($(this).val());
		// })
		//登录按钮
		$("button[id='btn_login']").click(function(event) {
		//获取是学生还是管理员
		let Lgin_RadOp = $("input[name='login_inlineRadioOptions']:checked").val();
		console.log(Lgin_RadOp);
		// $("input[name='login_inlineRadioOptions']").change(function(event) {
		// 	Lgin_RadOp = $(this).val();
		// });
		//获取输入框
			login_account=$("input[id='login_account']").val();
			console.log(login_account);
			login_password=$("input[id='login_password']").val();
			if (login_account.length === 0) {
				alert("请输入账号");
				return;
			}
			if (Lgin_RadOp === null) {
				alert("请选择类型");
				return;
			}
			if (login_password.trim().length < 3) {
				alert("密码至少要求3位");
				return;
			}
			//区分登录用户的类型
			let result = false;
			if (Lgin_RadOp === "student") {
				for (let index in studentarr) {
					item = studentarr[index];
					if (item === null) {
						continue;
					}
					console.log(parseInt(item.password), login_password);
					if (item.name === login_account && item.password=== login_password) {
						result = true;
					}
				}
			}
			if (Lgin_RadOp === "administrators") {
				console.log(11);
				for (let index in adminarr) {
					item = adminarr[index];
					if (item === null) {
						continue;
					}
					// console.log(item.name === login_account);
					// console.log(parseInt(item.password) === login_password);
					if (item.name === login_account && item.password=== login_password) {
						result = true;
					}
				}
			}
			console.log(Lgin_RadOp)
			if (Lgin_RadOp === "admin") {
				if (login_account === "admin" && parseInt(login_password) === 123456) {
					result = true;
				}
			}
			//在此判断他们是否登录成功
			console.log(result);
			if (result) {
				
				//把当前用户的类型和用户名密码都打包
				let current_user={
					cu_user:login_account,
					cu_password:login_password,
					yhlx:Lgin_RadOp
				}
				//将用户账号存到sessionStorage中
				console.log("成功了！");
				sessionStorage.setItem("current_user",JSON.stringify(current_user) );
				console.log($("input[name='cb_rem']:checked").val())
				if ($("input[name='cb_rem']:checked").val()) {
					localStorage.setItem("remccount",JSON.stringify(current_user) );
				}else{
					localStorage.removeItem("remccount");
				}
				//页面跳转
				location.href = "index.html";
			} else{
				alert("账号或者密码错误");
			}

		})

		/*
		注册的js*/
		let RadOp = null;
		$("input[name='reg_inlineRadioOptions']").change(function(event) {
			RadOp = $(this).val();
		})
		$("button[id='sava']").click(function(event) {
			let n_user = $("input[id='n_user']").val();
			let Telephone = $("input[id='Telephone']").val();
			let password = $("input[id='password']").val();
			let se_password = $("input[id='se_password']").val();
			console.log(RadOp);
			if ("adimer"=== n_user) {
				alert("该账号不能注册！");
				return;
			}
			if (RadOp === "student") {
				if (n_user.trim().length === 0) {
					alert("请输入账号");
					return;
				}
				if (password.trim().length < 3) {
					alert("密码至少要求3位");
					return;
				}
				if (password !== se_password) {
					alert("两次密码必须一致");
					return;
				}
				
				for (let index in studentarr) {
					let item = studentarr[index];
					console.log("adimer"=== n_user)
					console.log(item.name === n_user)
					console.log(item.name === n_user);
					// item.name === n_user && "adimer"=== n_user
					if (item.name === n_user) {
						alert("该账号以被注册");
						return;
					}
				}
				let tem = 0;
				for (let item of studentarr) {
					let id = item.id;
					if (tem < id) {
						tem = id;
					}
				}
				let obj = {
					id: tem + 1,
					name: n_user,
					Fullname: "子鱼",
					Gender: "男",
					address: "1栋",
					dorm: 212,
					Telephone: Telephone,
					password: password
				}
				console.log(obj);
				studentarr.push(obj);
				localStorage.setItem("studentarr", JSON.stringify(studentarr)); //保存到localStorage中
				alert("注册成功");
			} else if (RadOp === "ators") {
				if (n_user.trim().length === 0) {
					alert("请输入账号");
					return;
				}
				if (password.trim().length < 6) {
					alert("密码至少要求6位");
					return;
				}
				if (password !== se_password) {
					alert("两次密码必须一致");
					return;
				}
				for (let index in adminarr) {
					let item = adminarr[index];
					//为什么&&“admin”===n_user
					if (item.name === n_user ) {
						alert("该账号以被注册");
						return;
					}
				}
				let tem = 0;
				for (let item of adminarr) {
					let id = item.id;
					if (tem < id) {
						tem = id;
					}
				}
				let obj = {
					id: tem + 1,
					name: n_user,
					Gender:"男",
					address:"1栋",
					Telephone: Telephone,
					password: password
				}
				console.log(obj);
				adminarr.push(obj);
				localStorage.setItem("adminarr", JSON.stringify(adminarr)); //保存到localStorage中
				alert("注册成功");
			}else{
				alert("请选择类型");
			}
			console.log(se_password, password, Telephone);
		});
	})
